<?php
namespace app\home\controller;
use app\home\model\Systemt;
// use app\html\model\Filex;
use app\home\model\Channel;
use think\Controller;
use think\Request;
use think\Config;
use think\Db;
use think\captcha\Captcha;
// use app\index\
class Base extends Controller
{
    protected $meta;
    public function _initialize()
    {
        $systemtModel=new Systemt();
        $systems=$systemtModel->getSystem();
        // $systems['tel']=db("cms_contact")->where("id=1")->value("tel");

        $channelModel=new Channel();
        $channel=$channelModel->getChannel();
        $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        // SEO 关键字
        $meta=$this->meta;
        $meta['title']=$systems['title'];
        $meta['keywords']=$systems['keywords'];
        $meta['descript']=$systems['descript'];

        $nowtime=date("Y-m-d H:i:s",time());

        // 菜单栏目
        // $channel=$this->get_channel();

        $fragcate=db("plugin_advert_category")->where("status=1")->order("sort asc,id desc")->select();
        for ($i=0; $i < count($fragcate); $i++) {
            $childs=db("plugin_advert")->where("status=1 and cid=".$fragcate[$i]['id'])->select(); 
         
            $fragments[$fragcate[$i]['id']]=array_column($childs,null,"id");
        }
        $addonMess=$this->get_addons_mess();
        // print_r($fragments);exit;
        $this->assign("addonMess",$addonMess);        
        $this->assign("fragments",$fragments);        
        $this->assign("meta",$meta);
        $this->assign("pagebanner",$pagebanner);
        $this->assign("systems",$systems);
        $this->assign("channel",$channel);
        $this->assign("nowtime",$nowtime);
    }
    public function get_addons_mess(){
        //功能组件
        //1.服务项目
        $service=db("plugin_service")->where("status = 1")->order("sort asc, id desc")->select();
        $mess['service']=$service;
        //2.普通组件
        $general=db("plugin_general")->where("status = 1")->order("sort asc, id desc")->select();
        $mess['general']=$general;
        //3.发展历程
        $history=db("plugin_history")->where("status = 1")->order("occurtime desc,sort asc, id desc")->select();
        $mess['history']=$history;
        //4.联系方式
        $contact=db("plugin_contact")->find(1);
        $mess['contact']=$contact;
        //5.广告管理
        $fragcate=db("plugin_advert_category")->where("status=1")->order("sort asc,id desc")->select();
        for ($i=0; $i < count($fragcate); $i++) {
            $childs=db("plugin_advert")->where("status=1 and cid=".$fragcate[$i]['id'])->select(); 
         
            $fragments[$fragcate[$i]['id']]=array_column($childs,null,"id");
        }
        $mess['adverts']=$fragments;
        //6.荣誉资质
        $honor=db("plugin_honor")->where("status = 1")->order("sort asc, id desc")->select();
        $mess['honor']=$honor;
        //7.友情链接
        $link=db("plugin_link")->where("status = 1")->order("sort asc, id desc")->select();
        $mess['link']=$link;
        //8.合作伙伴
        $partner=db("plugin_partner")->where("status = 1")->order("sort asc, id desc")->select();
        $mess['partner']=$partner;

        //新开发组件后，请按照以上规则，将数据查询出来封装至$mess数组中，渲染至页面
        //…………
        return $mess;
    }
    public function getSeoMeta($gnavName,$pnavName,$mess=''){
        $meta=$this->meta;
        if($mess){
            $meta['title']=$mess['title'].' '.$pnavName.' '.$gnavName.' '.$meta['title'];
            if($mess['keywords']){
                $meta['keywords']=$mess['keywords'];
            }else{
                $meta['keywords']=$mess['title'].$pnavName.$gnavName.$meta['keywords'];
            }
            if($mess['descript']){
                $meta['descript']=$mess['descript'];
            }else{
                $meta['descript']=$mess['title'].$pnavName.$gnavName.$meta['descript'];
            }
        }else{
            $meta=$this->meta;
            $meta['title']=$pnavName.' '.$gnavName.' '.$meta['title'];

            $meta['keywords']=$pnavName.$gnavName.$meta['keywords'];

            $meta['descript']=$pnavName.$gnavName.$meta['descript'];

        }
        
        return $meta;
    }
    public function getIndexPage($cid){
        $tempid=db("channel")->where("id=".$cid)->value("template");        
        if($tempid){
            $viewPage=db("template")->where("id=".$tempid)->value("vpage");
        }else{
            $viewPage="index";
        }
        return $viewPage;
    }
    public function getDetailPage($cid){
        $viewPage="detail";
        $tempid=db("channel")->where("id=".$cid)->value("template");        
        if($tempid){
            $vpage=db("template")->where("id=".$tempid)->value("vpage");
            if($vpage && $vpage != 'index'){
                $viewPage=$vpage."_detail";
            }  
        }
        return $viewPage;
    }
    public function getData($mark,$where,$page){
        if($mark != 'index'){
            $getModel=new $mark();
        }
    }
    public function get_channel(){  
        // $where['type']=1;
        $where['status']=1;
        $modelx=db("modelx");
        $channel=db("channel")->where($where)->order("sort asc , id asc")->select();
        
        for ($i=0; $i < count($channel); $i++) { 
            $channel[$i]['contro']=$modelx->where("id=".$channel[$i]['modelx'])->value("mark");
            $channel[$i]['level']=$modelx->where("id=".$channel[$i]['modelx'])->value("level");
            $channel[$i]['type']=$modelx->where("id=".$channel[$i]['modelx'])->value("type");
        }
        $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        return $channel;
    }
    public function get_left_channel($pid){
        // $where['type']=1;
        $where['status']=1;
        $where['pid']=$pid;
        $modelx=db("modelx");
        $lchannel=db("channel")->where($where)->order("sort asc , id asc")->select();
        
        for ($i=0; $i < count($lchannel); $i++) { 
            $lchannel[$i]['contro']=$modelx->where("id=".$lchannel[$i]['modelx'])->value("mark");
            // $lchannel[$i]['vpage']=db("template")->where("id=".$lchannel[$i]['template'])->value('vpage');
            // if(!$lchannel[$i]['vpage']){
            //     $lchannel[$i]['vpage']="index";
            // }
        }
        // $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        return $lchannel;
        // $this->assign("channel",$channel);
    }
    public function get_banner($id){
        // $where['type']=1;
        $where['id']=$id;
        $banner=db("channel")->where($where)->value("thumb");
           
        return $banner;
        // $this->assign("channel",$channel);
    }
    
    public function get_product_cate($cid){
        $cate=db("channel")->where("pid=".$cid)->order("sort asc , id DESC")->select();
        for ($i=0; $i < count($cate); $i++) { 
            $cate[$i]['child']=db("channel")->where("pid=".$cate[$i]['id'])->select();
        }        
        
        return $cate;
    }
    public function get_table_name($cid){
        $moxing=db("menu")->where("id=".$cid)->value("moxing");
        // print_r($moxing);exit;
        $table_name=db("moxing")->where("id=".$moxing)->value("table_name");

        $table_pre=config('database.prefix');
        $prefix_length=strlen($table_pre);

        // $table_name=$this->get_mark_table($id);
        // print_r($prefix_length);exit;
        $table_name=substr($table_name,$prefix_length);
        
        return $table_name;
    }
    
    
    public function upload_file(){
        // $file=$_FILES['file'];
        $file = request()->file('file');
        // $post=$_POST;
        // return json_encode($post);
        // print_r($file);exit();
        $mess=$this->upload($file);
        return json_encode($mess);
    }
    public function upload($file){
        // 获取表单上传文件 例如上传了001.jpg
        // $file = request()->file('image');
        
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            // $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $filename=$info->getSaveName();
                $mess['code']=1;
                $mess['msg']="上传成功";
                $mess['filename']=$filename;
                return $mess;
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // return $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                $error=$file->getError();
                $mess['code']=0;
                $mess['msg']=$error;
                $mess['error']=$error;
                return $mess;
            }
        }
    }
    public function showVerify()
    {
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    30,    
            // 验证码位数
            // 'length'      =>    3,   
            // 关闭验证码杂点
            'useNoise'    =>    false, 
            // 'bg'          =>    false
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();    
    }
    public  function  getMenu($data,$pid=0,$deep=0)
    {
        $tree=array();
        foreach ($data as $row) {
            if($row['pid']==$pid){
                $row['child']=$this->getMenu($data,$row['id'],$deep+1);
                $tree[]=$row;
            }
        }
        return $tree;
    }

    // 按照顺序构造一级数组，根据level判断层级
    public function getTree($array, $pid =0, $level = 0){

        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level+1);

            }
        }
        return $list;
    }
    function makeTree($list, $pk='id', $pid = 'pid', $child = 'child', $root = 0) {
      // 创建Tree
      $tree = array();
      if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
          $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
          // 判断是否存在parent
          $parentId = $data[$pid];
          if ($root == $parentId) {
            $tree[] =& $list[$key];
          }else{
            if (isset($refer[$parentId])) {
              $parent =& $refer[$parentId];
              $parent[$child][] =& $list[$key];
            }
          }
        }
      }
      return $tree;
    }
}
